#import "SettingViewController.h"
#import "PersonalInfoViewController.h"
#import "InformViewController.h"
#import "UserData.h"
#import <AFNetworking/AFNetworking.h>
#import "LoginController.h"
#import "MessageViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *thirdArr;
    NSArray *fourArr;
}
@property (nonatomic,strong)UITableView *tableview;
@end

@implementation SettingViewController

- (instancetype)init
{
    self = [super init];
    
    self.tabBarItem.title = @"设置中心";
    self.tabBarItem.image = [UIImage imageNamed:@"user@2x.png"];
    self.tabBarItem.selectedImage =[UIImage imageNamed:@"user-filled@2x.png"];

    return self;
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

        _tableview.delegate = self;
        _tableview.dataSource = self;
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftBtnPress:)];
    self.navigationItem.leftBarButtonItem = leftBtn;

    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(rightBtnPress:)];
    self.navigationItem.rightBarButtonItem = rightBtn;

       //添加表格
    [self.view addSubview:self.tableview];

    thirdArr = @[@"查看通知"];
}
#pragma mark - 按钮触发
- (void)leftBtnPress:(id)sender{

}

- (void)rightBtnPress:(id)sender{
    [self LogOut];
}

- (void)LogOut
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:@"确定要退出登录？"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *URL = @"http://172.18.178.56/api/user/logout";
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        [manager POST:URL parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable response) {
            
            // 找到主界面，更改根vc
            UIWindow* window = nil;
            window = [UIApplication sharedApplication].keyWindow;
            window.rootViewController =[LoginController new];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Log out failed somehow");
        }];
    }]];
    
    
    
    // 显示对话框
    [self presentViewController:alert animated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    else if (section == 1){
        return 1;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    UserData* user = [UserData shareUser];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    if (indexPath.section == 0) {
        cell.imageView.image = [UIImage imageNamed:@"edvard-munch.png"];
        cell.textLabel.text = user.Name;
        cell.detailTextLabel.text = user.Email;
    }
    else if (indexPath.section == 1){
        cell.textLabel.text = thirdArr[indexPath.row];
    }
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){
        PersonalInfoViewController *RecordVC =  [[PersonalInfoViewController alloc] init];
        [UIView transitionWithView:self.navigationController.view  duration:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
            CATransition *transition = [CATransition animation];
            transition.duration = 0.0f;
            transition.type = kCATransitionPush;
            transition.subtype = kCATransitionFromBottom;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
            [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
            [[self navigationController] pushViewController:RecordVC animated:YES];
        } completion:nil];
    }
    if(indexPath.section == 1){
        if(indexPath.row == 0){
            MessageViewController * infoVC = [[MessageViewController alloc] init];
            [UIView transitionWithView:self.navigationController.view  duration:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
                CATransition *transition = [CATransition animation];
                transition.duration = 0.0f;
                transition.type = kCATransitionPush;
                transition.subtype = kCATransitionFromBottom;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
                [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
                [[self navigationController] pushViewController:infoVC animated:YES];
            } completion:nil];
        }
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [UserData UpdateUser];
    [self.tableview reloadData];
}

@end

